package com.orbitdevs.testgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.orbitdevs.testgame.graphics.BaseSprite;
import com.orbitdevs.testgame.graphics.SmartCamera;
import com.orbitdevs.testgame.graphics.SpriteSheet;
import com.orbitdevs.testgame.input.TouchScreen;
import com.orbitdevs.testgame.map.Map;

/*To-do list(unordered):
 * NPCs
 * Special tiles, such as spawn points and enemy terretories
 * Multi finger input
 * Bullet class w rect-collision detection.
 * Simple particle class, for smoke and such
 * Sprite animations
 * Menu system with option part to enable disable graphics enhancements(particles etc.)
 * Decide on a tilesize/playersize
 * 
 * Make samuel(me) comment his goddamn code.
 * And also - move to a non-opensource repo. Things are getting serious. RasPI server?
 */



public class TestGame implements ApplicationListener{
	private SmartCamera camera;
	private SpriteBatch batch;
	private FPSLogger fps;
	private TouchScreen touch;
	private SpriteSheet sheet;
	private Map currentMap;
	private BaseSprite player;
	float TILEHEIGHT, TILEWIDTH;
	float SCREENWIDTH, SCREENHEIGHT;
	private BitmapFont font;
	private GestureDetector gestures;
	
	@Override
	public void create() {
		SCREENWIDTH = Gdx.graphics.getWidth();
		SCREENHEIGHT = Gdx.graphics.getHeight();

		TILEHEIGHT = SCREENHEIGHT / 7; // YOLOSWAG
		TILEWIDTH = SCREENWIDTH / 12; // COOLDAWG

		sheet = new SpriteSheet("data/spritesheet.png");
		fps = new FPSLogger();
		camera = new SmartCamera(SCREENWIDTH, SCREENHEIGHT);
		batch = new SpriteBatch();
		font = new BitmapFont();
		font.setScale(1.5f);
		touch = new TouchScreen(SCREENWIDTH, SCREENHEIGHT);
		currentMap = new Map(TILEWIDTH, TILEHEIGHT);
		player = new BaseSprite(sheet.getTexArray(0, 0, 64, 2, 3), 1, TILEWIDTH * 4,
				TILEHEIGHT * 6,TILEWIDTH * 0.7f, TILEHEIGHT * 0.9f, 0, 0);

	}

	@Override
	public void dispose() {
		batch.dispose();
		sheet.texture.dispose();
	}

	@Override
	public void render() {
		float dt = Gdx.graphics.getDeltaTime();
		fps.log();
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		batch.setProjectionMatrix(camera.combined);

		currentMap.renderer.render(camera);
		
		touch.updateTouch();
		eventHandler(touch.events);
		checkCollision(dt, player);
		player.update(dt);

		camera.smoothMove(player.getxPos(), player.getyPos(), dt);
		camera.update();

		batch.begin();
		font.setColor(Color.BLACK);
		/*font.draw(
				batch,
				"Pos: " + player.getxPos() + " , "
						+ currentMap.getTilePos(player.getMBot(dt))[0] + " "
						+ player.getyPos(), camera.getxPos(), camera.getyPos());*/
		player.sprite.draw(batch);
		batch.end();
	}

	@Override
	public void resize(int width, int height) {
	}

	public void checkCollision(float dt, BaseSprite sprite) {
		int[] innerPoints = currentMap.getPointIDs(currentMap, sprite, dt)[0];
		int[] outerPoints = currentMap.getPointIDs(currentMap, player, dt)[1];
		if (!(innerPoints[5] > 0) && !(outerPoints[5] > 0)) {
			sprite.setOnBlock(false ,0);
		}
		else if (innerPoints[5] == 1) {
			sprite.setOnBlock(true, currentMap.getTileTop(sprite.getMBot(dt)));			
		} 
		if (innerPoints[0] > 0 || innerPoints[1] > 0) {
			sprite.setLBlocked(true, currentMap.getTileRight(sprite.getBLeft(dt)));
		}
		else if (!(innerPoints[0] > 0) && !(innerPoints[1] > 0)) {
			sprite.setLBlocked(false, 0);
		}
		if (innerPoints[3] > 0 || innerPoints[4]> 0) {
			sprite.setRBlocked(true, currentMap.getTileLeft(sprite.getBRight(dt)));
		}
		else if (!(innerPoints[3]> 0) && !(innerPoints[4] > 0)) {
			sprite.setRBlocked(false, 0);
		}
		if(innerPoints[2] > 0){
			sprite.setUnderBlock(true, currentMap.getTileBottom(sprite.getMTop(dt)));
		}
		else if(!(innerPoints[2] > 0)){
			sprite.setUnderBlock(false, 0);
		}
	}
	
	public void eventHandler(Array<String> events){
		for(String s : events){
			if(s == "UP"){
				player.setyVel(1000);
			}
			if(s == "RIGHT"){
				player.setxVel(700);
			}
			if(s == "LEFT"){
				player.setxVel(-700);
			}
			if(s == "STOP"){
				player.setxVel(0);
			}
		}
		events.clear();
		
	}

	@Override
	public void pause() {
		
	}

	@Override
	public void resume() {

	}

}
